package com.orifine.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.sql.DataSource;

import org.logicalcobwebs.proxool.ProxoolDataSource;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

import com.bowos.zmado.dao.UserDao;
import com.bowos.zmado.service.UserManager;

public class Test implements BeanFactoryPostProcessor, InitializingBean {
	public static void main(String[] args) throws Exception {
		ProxoolDataSource targetDataSource = new ProxoolDataSource();
		targetDataSource.setDriver("com.mysql.jdbc.Driver");
		targetDataSource.setDriverUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8");
		targetDataSource.setPrototypeCount(5);
		targetDataSource.setUser("root");
		targetDataSource.setPassword("root");
		targetDataSource.setMaximumConnectionCount(2);
		targetDataSource.setMinimumConnectionCount(2);
		
		DataSource dataSource = targetDataSource;
		Connection connection = dataSource.getConnection();
//		connection.close();
		
//		connection = dataSource.getConnection();
//		System.out.println(connection);
//
//		connection = dataSource.getConnection();
//		System.out.println(connection);
//		connection.close();
//
//		connection = dataSource.getConnection();
//		System.out.println(connection);
//
//		connection = dataSource.getConnection();
//		System.out.println(connection);
//		connection.close();
//
//		connection = dataSource.getConnection();
//		System.out.println(connection);
//		connection.close();
		Statement statement = connection.createStatement();
		ResultSet resultSet = statement.executeQuery("select id as '@id', email as '@2id', username as user,password as psd from user");
		
		System.out.println(resultSet.getMetaData().getColumnLabel(1));
		
//		statement.execute("insert into friendly_link(id, address, name) values(42, 'abc', 'hel\\\\lo');");
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException {
		System.out.println(userDao);
	}

	@Autowired(required=true)
	private UserDao userDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	@Autowired
	private UserManager userService;

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println(userDao);
		System.out.println(userService);
	}
}
